package com.sky.controller.user;

import com.sky.dto.ConfirmDTO;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Api(tags = "地址簿")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBookById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return Result.success(addressBook);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        AddressBook addressBook = addressBookService.getDefault();
        return Result.success(addressBook);
    }

    @ApiOperation("查询当前登录用户的所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAll() {
       List<AddressBook> addressBooks = addressBookService.getAll();
       return Result.success(addressBooks);
    }

    @ApiOperation("根据id修改地址")
    @PutMapping()
    public Result updateAddressBookById(@RequestBody AddressBook addressBook)  {
         addressBookService.update(addressBook);
        return Result.success();
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefaultAddressBook(@RequestBody ConfirmDTO confirmDTO) {
        addressBookService.setDefault(confirmDTO.getId());
        return Result.success();
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping()
    public Result delete(@RequestParam Long id) {
        addressBookService.delete(id);
        return Result.success();
    }

    @ApiOperation("新增地址")
   @PostMapping()
    public Result add(@RequestBody AddressBook addressBook) {
      log.info("新增地址:{}",addressBook);
      addressBookService.add(addressBook);
        return Result.success();
    }
}
